﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySoft.Data;
using Caifu.Fx;
using Itfort;
namespace Caifu.Datas
{

	public class UserData
	{
		/// <summary>
		/// User数据操作类；
		/// </summary>
		public static readonly UserData Instance = new UserData();

		private readonly DbSession dbSession = new DbSession("Caifu");

		/// <summary>
		/// 检测用户名的存在性;
		/// </summary>
		/// <returns>true:存在冲突，false:不存在冲突</returns>
		public Boolean ConflictByName(String username)
		{
			var user = dbSession.From<User>()
			.Where(User._.UserName == username).ToSingle<User>();
			return user != null ? true : false;
		}

		/// <summary>
		/// 检测邮箱的存在性;
		/// </summary>
		/// <returns>true:存在冲突，false:不存在冲突</returns>
		public Boolean ConflictByEmail(String email)
		{
			var user = dbSession.From<User>()
			.Where(User._.Email == email).ToSingle<User>();
			return user != null ? true : false;
		}

		/// <summary>
		/// 添加用户
		/// </summary>
		public void AddUser(User user)
		{
			user.Detach();
            user.Id = Comb.NewComb();
            user.UpdateTime = DateTime.Now;
            user.AddTime = DateTime.Now;
			dbSession.Save<User>(user);
		}

		/// <summary>
		/// 修改密码
		/// </summary>
		/// <param name="email">用户邮箱</param>
		/// <param name="oldPwd">原密码</param>
		/// <param name="newPwd">新密码</param>
		public void ModifyPassword(String email, String oldPwd, String newPwd)
		{
			var user = GetUserByEmail(email);
			if (user != null)
			{
				if (user.PasswordHash.Equals(MD5Hash.ComputeHash(oldPwd)))
				{
					user.Attach();
					user.UpdateTime = DateTime.Now;
					user.PasswordHash = MD5Hash.ComputeHash(newPwd);
					dbSession.Save<User>(user);
				}
				else
				{
					throw new CaifuException("原密码错误，修改失败");
				}
			}
			else
			{
				throw new CaifuException("该用户不存在，修改失败");
			}
		}

		/// <summary>
		/// 根据邮箱返回用户
		/// </summary>
		/// <param name="email">邮箱</param>
		/// <returns>用户</returns>
		public User GetUserByEmail(String email)
		{
			return dbSession.Single<User>(User._.Email == email);
		}

		/// <summary>
		/// 修改用户昵称
		/// </summary>
		/// <param name="email">用户邮箱</param>
		/// <param name="username"></param>
		public void ModifyNickname(String email, String nickname)
		{
			var user = GetUserByEmail(email);
			if (user != null)
			{
				user.Attach();
				user.UpdateTime = DateTime.Now;
				user.UserName = nickname;
				dbSession.Save<User>(user);
			}
			else
			{
				throw new CaifuException("该用户不存在，修改失败");
			}
		}

		/// <summary>
		/// 根据ID获得用户信息；
		/// </summary>
		/// <param name="id">
		/// id 用户主键标示；
		/// </param>
		/// <returns></returns>
		public User GetUserById(Guid id)
		{
			return dbSession.Single<User>(User._.Id == id);
		}

		/// <summary>
		/// 用户登录
		/// </summary>
		/// <param name="mail">用户邮箱</param>
		/// <param name="password">密码</param>
		/// <returns>True :登录成功，False：登录失败</returns>
		public User Login(String mail,String password)
		{
            //这里所指的用户名实际为邮箱
			var conditions = User._.Email == mail && User._.Password == password;
			return dbSession.From<User>()
				.Where(conditions).ToSingle<User>();
		}
	}
}
